export const getSuffix = (fileName: string) => {
  return fileName.slice(fileName.lastIndexOf(".") + 1);
};
export const getFileName = (fileName: string) => {
  return fileName.slice(0, fileName.lastIndexOf("."));
};
/**
 * 获取节点的层级
 * @param tree
 * @param id
 * @param level
 */
export const findNodeById = (tree: Array<any>, id: string, level = 1) => {
  for (const node of tree) {
    if (node.reformTypeId === id) {
      return level;
    }
    if (node.children && node.children.length > 0) {
      const foundLevel: any = findNodeById(node.children, id, level + 1);
      if (foundLevel !== undefined) {
        return foundLevel;
      }
    }
  }
  return undefined;
};

/**
 * 默认展开第一个的 level 3 节点的 id
 * @param treeData
 * @param currentLevel
 */
export const findFirstLevel3Id = (treeData: Array<any>, currentLevel = 1) => {
  for (const node of treeData) {
    if (currentLevel === 3) {
      return node.reformTypeId;
    } else if (node.children && node.children.length > 0) {
      const result: any = findFirstLevel3Id(node.children, currentLevel + 1);
      if (result !== undefined) {
        return result;
      }
    }
  }
  return undefined;
};
/**
 * 默认展开第一个的 末节点的 id
 * @param treeData
 * @param currentLevel
 */
export const findFirstLevelId = (tree: Array<any>, path = "reformTypeId") => {
  // 调用递归函数，开始遍历树形结构
  return traverse(tree[0], path);
};
// 递归函数，用于遍历树形结构
const traverse = (node: any, path: any): any => {
  if (!node.children || node.children.length < 1) {
    return node.reformTypeId;
  }
  // 如果当前节点是数组
  else if (node.children && Array.isArray(node.children)) {
    let arr = node.children;
    let i = 0;
    // 如果子元素是对象，则递归查找
    if (!arr[i].children || arr[i].children.length < 1) {
      return arr[i][path];
    } else {
      return traverse(arr[i], path);
    }
  }
};
